package lms.dwr.demo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lms.dwr.demo.common.DAOException;
import lms.dwr.demo.common.ServiceException;
import lms.dwr.demo.dao.DAOSystemOption;
import lms.dwr.demo.model.ModelSystemOption;
import lms.dwr.demo.service.ServiceSystemOption;
import lms.dwr.demo.web.component.pagination.PaginationSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * The implementation of system option service
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceSystemOptionImpl
extends ServiceBaseImpl implements ServiceSystemOption
{
	
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ServiceSystemOptionImpl.class);
	
	/**
	 * The system option repository
	 */
	private DAOSystemOption daoSystemOption;
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOptionListByEntity(com.lzcp.model.ModelSystemOption)
	 */
	public List<ModelSystemOption> getModelSystemOptionListByEntity(
			ModelSystemOption entity, String order, Boolean isDesc) throws ServiceException
	{
		if (entity != null)
		{
			DetachedCriteria criteria = DetachedCriteria.forClass(ModelSystemOption.class);
			
			if (StringUtils.isNotEmpty(entity.getOptionSlug()))
			{
				criteria.add(Restrictions.eq("optionSlug", entity.getOptionSlug()));
			}
			
			if (StringUtils.isNotEmpty(entity.getOptionName()))
			{
				criteria.add(Restrictions.eq("optionName", entity.getOptionName()));
			}
			
			if (StringUtils.isNotEmpty(entity.getOptionValue()))
			{
				criteria.add(Restrictions.eq("optionValue", entity.getOptionValue()));
			}
			
			if (entity.getOptionVisible() != null)
			{
				criteria.add(Restrictions.eq("optionVisible", entity.getOptionVisible()));
			}
			
			if(StringUtils.isNotBlank(order)) 
			{
				if(isDesc == null||!isDesc) 
				{
					criteria.addOrder(Order.asc(order));
				}
				else
				{
					criteria.addOrder(Order.desc(order));
				}
			}
			else
			{
				criteria.addOrder(Order.desc("optionCreateTime"));
			}
			
			try 
			{
				return daoSystemOption.getListByCriteria(criteria);
			} 
			catch (DAOException e) 
			{
				LOGGER.error("obtains system option by entity failed.", e);
				return null;
			} 
			catch (Exception e)
			{
				throw (new ServiceException("obtains system option by entity failed.", e));
			}
			
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOptionListBySlug(java.lang.String)
	 */
	@Override
	public List<ModelSystemOption> getModelSystemOptionListBySlug(
			String optionSlugName) throws ServiceException
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelSystemOption.class);
		
		criteria.add(Restrictions.eq("optionSlug", optionSlugName));
		criteria.addOrder(Order.asc("id"));
		
		try 
		{
			return daoSystemOption.getListByCriteria(criteria);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("obtains system option by slug name failed: " + optionSlugName, e);
			return null;
		} 
		catch (Exception e)
		{
			throw (new ServiceException("obtains system option by slug name failed: " + optionSlugName, e));
		}
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOptionListBySlugAndName(java.lang.String, java.lang.String)
	 */
	@Override
	public List<ModelSystemOption> getModelSystemOptionListBySlugAndName(
			String optionSlugName, String optionName) throws ServiceException
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelSystemOption.class);
		
		criteria.add(Restrictions.eq("optionSlug", optionSlugName));
		criteria.add(Restrictions.eq("optionName", optionName));
		criteria.addOrder(Order.desc("optionCreateTime"));
		
		try 
		{
			return daoSystemOption.getListByCriteria(criteria);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("obtains system option by slug and name failed: " + optionSlugName + "," + optionName, e);
			return null;
		} 
		catch (Exception e)
		{
			throw (new ServiceException("obtains system option by slug and name failed: " + optionSlugName + "," + optionName, e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceSystemOption#getModelSystemOptionListBySlugAndValue(java.lang.String, java.lang.String)
	 */
	public List<ModelSystemOption> getModelSystemOptionListBySlugAndValue(String optionSlugName,
			String optionValue) throws ServiceException
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelSystemOption.class);
		
		criteria.add(Restrictions.eq("optionSlug", optionSlugName));
		
		if (StringUtils.isNotBlank(optionValue))
		{
			criteria.add(Restrictions.like("optionValue", optionValue, MatchMode.ANYWHERE));
		}
		
		criteria.addOrder(Order.desc("optionCreateTime"));
		
		try 
		{
			return daoSystemOption.getListByCriteria(criteria);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("obtains system option by slug and name failed: " + optionSlugName + "," + optionValue, e);
			return null;
		} 
		catch (Exception e)
		{
			throw (new ServiceException("obtains system option by slug and name failed: " + optionSlugName + "," + optionValue, e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOptionPageBySlug(java.lang.String, int, int, java.lang.String, java.lang.Boolean)
	 */
	@Override
	public PaginationSupport<ModelSystemOption> getModelSystemOptionPageBySlug(
			String optionSlugName, int pageSize, int startIndex, String order,
			Boolean isDesc) throws ServiceException
	{
		
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelSystemOption.class);
		
		criteria.add(Restrictions.eq("optionSlug", optionSlugName));
		
		if(StringUtils.isNotBlank(order)) 
		{
			if(isDesc == null||!isDesc) 
			{
				criteria.addOrder(Order.asc(order));
			}
			else
			{
				criteria.addOrder(Order.desc(order));
			}
		}
		else
		{
			criteria.addOrder(Order.desc("optionCreateTime"));
		}
		
		try 
		{
			return daoSystemOption.findPageByCriteria(criteria, pageSize, startIndex);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("obtains system option with pagination by slug failed: " + optionSlugName, e);
			return null;
		} 
		catch (Exception e)
		{
			throw (new ServiceException("obtains system option with pagination by slug failed: " + optionSlugName, e));
		}
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOptionPageBySlugAndName(java.lang.String, java.lang.String, int, int, java.lang.String, java.lang.Boolean)
	 */
	@Override
	public PaginationSupport<ModelSystemOption> getModelSystemOptionPageBySlugAndName(
			String optionSlugName, String optionName, int pageSize,
			int startIndex, String order, Boolean isDesc)
			throws ServiceException
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelSystemOption.class);
		
		criteria.add(Restrictions.eq("optionSlug", optionSlugName));
		criteria.add(Restrictions.eq("optionName", optionName));
		
		if(StringUtils.isNotBlank(order)) 
		{
			if(isDesc == null||!isDesc) 
			{
				criteria.addOrder(Order.asc(order));
			}
			else
			{
				criteria.addOrder(Order.desc(order));
			}
		}
		else
		{
			criteria.addOrder(Order.desc("optionCreateTime"));
		}
		
		try 
		{
			return daoSystemOption.findPageByCriteria(criteria, pageSize, startIndex);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("obtains system option with pagination by slug and name failed: " + optionSlugName + "," + optionName, e);
			return null;
		} 
		catch (Exception e)
		{
			throw (new ServiceException("obtains system option with pagination by slug and name failed: " + optionSlugName + "," + optionName, e));
		}
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOptionMapBySlug(java.lang.String)
	 */
	@Override
	public Map<String, ModelSystemOption> getModelSystemOptionMapBySlug(
			String optionSlugName) throws ServiceException
	{
		List<ModelSystemOption> settingList = getModelSystemOptionListBySlug(optionSlugName);
		
		if(settingList != null && settingList.size() > 0)
		{
			Map<String, ModelSystemOption> settingMap = new HashMap<String, ModelSystemOption>();
			
			for(ModelSystemOption setting : settingList)
			{
				settingMap.put(setting.getOptionName(), setting);
			}
			
			return settingMap;
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOptionMapBySlugAndName(java.lang.String, java.lang.String)
	 */
	@Override
	public Map<String, ModelSystemOption> getModelSystemOptionMapBySlugAndName(
			String optionSlugName, String optionName) throws ServiceException
	{
		List<ModelSystemOption> settingList = getModelSystemOptionListBySlugAndName(optionSlugName, optionName);
		
		if(settingList != null && settingList.size() > 0)
		{
			Map<String, ModelSystemOption> settingMap = new HashMap<String, ModelSystemOption>();
			
			for(ModelSystemOption setting : settingList)
			{
				settingMap.put(setting.getOptionName(), setting);
			}
			
			return settingMap;
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#getModelSystemOption(java.lang.String)
	 */
	@Override
	public ModelSystemOption getModelSystemOptionById(String id)
			throws ServiceException
	{
		
		try 
		{
			if (StringUtils.isNotBlank(id)) 
			{
				return daoSystemOption.get(id);
			}
			
			return null;
		} 
		catch (DAOException e) 
		{
			LOGGER.error("obtains system option by id failed: " + id, e);
			return null;
		} 
		catch (Exception e)
		{
			throw (new ServiceException("obtains system option by id failed: " + id, e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#removeModelSystemOption(java.lang.String)
	 */
	@Override
	public boolean removeModelSystemOption(String id) throws ServiceException
	{
		return this.removeModelSystemOption(this.getModelSystemOptionById(id));
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#removeModelSystemOption(com.lzcp.model.ModelSystemOption)
	 */
	@Override
	public boolean removeModelSystemOption(ModelSystemOption modelSystemOption)
			throws ServiceException
	{
		if(modelSystemOption != null)
		{
			
			try 
			{
				daoSystemOption.remove(modelSystemOption);
				return true;
			} 
			catch (DAOException e) 
			{
				LOGGER.error("removes system option failed.", e);
				return false;
			} 
			catch (Exception e)
			{
				throw (new ServiceException("removes system option failed.", e));
			}
		}
		
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.service.ServiceSystemOption#saveModelSystemOption(com.lzcp.model.ModelSystemOption)
	 */
	@Override
	public String saveModelSystemOption(ModelSystemOption modelSystemOption)
			throws ServiceException
	{
		if(modelSystemOption != null)
		{
			try 
			{
				daoSystemOption.saveOrUpdate(modelSystemOption);
				return modelSystemOption.getId();
			} 
			catch (DAOException e) 
			{
				LOGGER.error("save/update system option failed.", e);
				return null;
			} 
			catch (Exception e)
			{
				throw (new ServiceException("save/update system option failed.", e));
			}
		}
		
		return null;
	}

	public static Logger getLogger()
	{
		return LOGGER;
	}
	
	public DAOSystemOption getDaoSystemOption()
	{
		return daoSystemOption;
	}

	public void setDaoSystemOption(DAOSystemOption daoSystemOption)
	{
		this.daoSystemOption = daoSystemOption;
	}

}
